﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class MeetingsTypesForm : Form
    {
        public MeetingsTypesForm()
        {
            InitializeComponent();
        }

        tb_meetingtype mType;
        bool insertRec = true;

        private void MeetingsTypesForm_Load(object sender, EventArgs e)
        {
            dgvMeetingTypes.DataSource = DBClass.MeetingTypes.Where(wh => wh.ID > 0).ToList();

            dgvMeetingTypes.Columns["ID"].Visible = false;
            dgvMeetingTypes.Columns["NameRus"].DisplayIndex = 0;
            dgvMeetingTypes.Columns["NameRus"].HeaderText = "Название(Рус)";
            dgvMeetingTypes.Columns["NameUzb"].DisplayIndex = 1;
            dgvMeetingTypes.Columns["NameUzb"].HeaderText = "Название(Узб)";
            dgvMeetingTypes.Columns["Quantity"].DisplayIndex = 2;
            dgvMeetingTypes.Columns["Quantity"].HeaderText = "Необходимое кол-во";
            dgvMeetingTypes.Columns["Visitors"].DisplayIndex = 3;
            dgvMeetingTypes.Columns["Visitors"].HeaderText = "Посещения";
            dgvMeetingTypes.Columns["IsConflict"].DisplayIndex = 4;
            dgvMeetingTypes.Columns["IsConflict"].HeaderText = "Конфликт";

            dgvMeetingTypes.Columns["IsSovet"].DisplayIndex = 5;
            dgvMeetingTypes.Columns["IsSovet"].HeaderText = "Совет";
            dgvMeetingTypes.Columns["IsCommission"].DisplayIndex = 6;
            dgvMeetingTypes.Columns["IsCommission"].HeaderText = "Ревизионная комиссия";


            //dgvMeetingTypes.Columns["NameRus"].HeaderText = "Название";
            //dgvMeetingTypes.Columns["Quantity"].HeaderText = "Количество";
            dgvMeetingTypes.AutoResizeColumns();

            dgvMeetingTypes.ReadOnly = true;
        }

        private void tsButtonAdd_Click(object sender, EventArgs e)
        {
            groupBox1.Enabled = true;
            buttonAddOrEdit.Text = "Добавить";
            insertRec = true;
            mType = null;
        }

        private void tsButtonEdit_Click(object sender, EventArgs e)
        {
            groupBox1.Enabled = true;
            buttonAddOrEdit.Text = "Редак.";
            insertRec = false;
            if (dgvMeetingTypes.RowCount > 0)
            {
                int typeID = (int)dgvMeetingTypes["ID", dgvMeetingTypes.CurrentRow.Index].Value;
                mType = DBClass.MeetingTypes.Where(wh => wh.ID == typeID).SingleOrDefault();
                tbNameRus.Text = mType.NameRus;
                tbNameUzb.Text = mType.NameUzb;
                rbIsVisit.Checked = mType.Visitors;
                rbIsConflict.Checked = mType.IsConflict;
                rbIsRevision.Checked = mType.IsCommission;
                rbIsSovet.Checked = mType.IsSovet;
                tbQuantity.Text = mType.Quantity.ToString();
            }
        }

        private void buttonAddOrEdit_Click(object sender, EventArgs e)
        {
            int quantity;

            if (Int32.TryParse(tbQuantity.Text, out quantity))
            {
                if (String.IsNullOrEmpty(tbNameRus.Text) || String.IsNullOrEmpty(tbNameUzb.Text))
                {
                    MessageBox.Show(msgClass.msgUncorrectData, msgClass.msgAchtung);
                    return;
                }

                if (insertRec && mType == null)
                {
                    mType = new tb_meetingtype
                    {
                        NameRus = tbNameRus.Text,
                        NameUzb = tbNameUzb.Text,
                        Quantity = quantity,
                        Visitors = rbIsVisit.Checked,
                        IsCommission = rbIsRevision.Checked,
                        IsConflict = rbIsConflict.Checked,
                        IsSovet = rbIsSovet.Checked
                    };
                    DBClass.MeetingTypes.InsertOnSubmit(mType);
                }
                else
                {
                    if (mType != null)
                    {
                        mType.NameRus = tbNameRus.Text;
                        mType.Quantity = quantity;
                        mType.NameUzb = tbNameUzb.Text;
                        mType.Visitors = rbIsVisit.Checked;
                        mType.IsCommission = rbIsRevision.Checked;
                        mType.IsConflict = rbIsConflict.Checked;
                        mType.IsSovet = rbIsSovet.Checked;
                    }
                }
                try
                {
                    if (DBClass.RESPContext.GetChangeSet().Deletes.Count > 0 || DBClass.RESPContext.GetChangeSet().Inserts.Count > 0 || DBClass.RESPContext.GetChangeSet().Updates.Count > 0)
                    {
                        DBClass.RESPContext.SubmitChanges();
                        MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                        dgvMeetingTypes.DataSource = DBClass.MeetingTypes.Where(wh => wh.ID > 0).ToList();
                    }
                }
                catch
                {
                    MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                }

            }
            else
            {
                if (InformationClass.Lang)
                    MessageBox.Show("Не верно введено количество");
                else
                    MessageBox.Show("Миқдори нотўғри киритилди");
                return;
            }
            tbNameRus.Text = tbQuantity.Text = tbNameUzb.Text = string.Empty;
            rbOther.Checked = true;
            groupBox1.Enabled = false;
        }

        private void tsButtonDelete_Click(object sender, EventArgs e)
        {
            string msg = InformationClass.Lang ? "Вы действительно хотате удалить собрание?" : "Сиз ҳақиқатдан ҳам йиғилишни олиб ташламоқчимисиз?";
            string titul = InformationClass.Lang ? "Удаление" : "Олиб ташланди";
            if (MessageBox.Show(msg, titul, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int typeID = (int)dgvMeetingTypes["ID", dgvMeetingTypes.CurrentRow.Index].Value;
                mType = DBClass.MeetingTypes.Where(wh => wh.ID == typeID).SingleOrDefault();
                DBClass.MeetingTypes.DeleteOnSubmit(mType);
                DBClass.RESPContext.SubmitChanges();
                dgvMeetingTypes.DataSource = DBClass.MeetingTypes.Where(wh => wh.ID > 0).ToList();
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            DBClass.RESPContext.SubmitChanges();
            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
